package com.xiaochong.tir.apiservice.scheduled;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.apiservice.component.HbaseBaseQueryComponent;
import com.xiaochong.tir.apiservice.config.ExecutorConfig;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.apiservice.service.KLineService;
import com.xiaochong.tir.common.data.hbasepo.KLineCmcData;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * Created by wujiaxing on 2018/6/14.
 */
@Component
public class KLineScheduled {

    @Autowired
    private KLineService kLineService;

    @Async
    @Scheduled(cron="0 0 0 * * ?")
    public void dayLineScheduled1() {
        LogTrace.beginTrace("0点定时任务dayLineScheduled1");
        LocalDateTime dateDate = LocalDateTime.now();
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED, LogTraceKeysUtils.DATA,"0点定时任务开始执行定时任务dayLineScheduled1");
        kLineService.dayLineScheduled(8 , KLineTypeEnum.HOUR83 , dateDate);
        kLineService.dayLineScheduled(12 , KLineTypeEnum.DAY_HALF2 , dateDate);
        kLineService.dayLineScheduled(24 , KLineTypeEnum.DAY , dateDate);
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED, LogTraceKeysUtils.DATA, "0点定时任务dayLineScheduled1定时执行完成");
        LogTrace.endTrace();
    }

    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 0 12 * * ?")
    public void dayLineScheduled12() {
        LogTrace.beginTrace("12点定时任务dayLineScheduled12");
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED12, LogTraceKeysUtils.DATA,"12点定时任务开始执行定时任务dayLineScheduled12");
        LocalDateTime dateDate = LocalDateTime.now();
        kLineService.dayLineScheduled(12 , KLineTypeEnum.DAY_HALF1 , dateDate);
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED12, LogTraceKeysUtils.DATA, "12点定时任务dayLineScheduled12定时执行完成");
        LogTrace.endTrace();
    }
//
    /**
     * 生成8小时的K线数据
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 0 8 * * ? ")
    public void dayLineScheduled8() {
        LogTrace.beginTrace("8点定时任务dayLineScheduled8");
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED8, LogTraceKeysUtils.DATA,"8点定时任务开始执行定时任务dayLineScheduled8");
        LocalDateTime dateDate = LocalDateTime.now();
        kLineService.dayLineScheduled(8 , KLineTypeEnum.HOUR81 , dateDate);
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED8, LogTraceKeysUtils.DATA, "8点定时任务dayLineScheduled8定时执行完成");
        LogTrace.endTrace();
    }


    /**
     * 生成8小时的K线数据
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 0 16 * * ? ")
    public void dayLineScheduled16() {
        LogTrace.beginTrace("16点定时任务dayLineScheduled8");
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED8, LogTraceKeysUtils.DATA,"16点定时任务开始执行定时任务dayLineScheduled16");
        LocalDateTime dateDate = LocalDateTime.now();
        kLineService.dayLineScheduled(8 , KLineTypeEnum.HOUR82 , dateDate);
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED8, LogTraceKeysUtils.DATA, "16点定时任务dayLineScheduled16定时执行完成");
        LogTrace.endTrace();
    }

    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void hoursKline(){
        LogTrace.beginTrace("小时数据K线生成");
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED8, LogTraceKeysUtils.DATA,"小时数据K线生成hoursKline");
        kLineService.klineDataHours();
        LogTrace.info(LogTraceKeysUtils.KLINE_SCHEDULED8, LogTraceKeysUtils.DATA, "小时数据K线生成hoursKline定时执行完成");
        LogTrace.endTrace();
    }
    //@Scheduled(cron="0 0/10 * * * ?")
   /* @Async
    @Scheduled(cron="0 10 0 1 * ?")
    public void monthLineScheduled() {
        LogTrace.beginTrace("每月第一天0点执行月k定时任务monthLineScheduled");
        LocalDateTime dateDate = LocalDateTime.now();
        while (dateDate.getDayOfMonth() != 1){
            dateDate = dateDate.minusDays(1);
        }
        LogTrace.info(LogTraceKeysUtils.KLINE_MONTH_SCHEDULED, LogTraceKeysUtils.DATA,"每月第一天0点执行月k定时任务monthLineScheduled");
        kLineService.monthLineScheduled(KLineTypeEnum.MONTH , dateDate);
        LogTrace.info(LogTraceKeysUtils.KLINE_MONTH_SCHEDULED, LogTraceKeysUtils.DATA, "每月第一天0点执行月k定时任务monthLineScheduled定时执行完成");
        LogTrace.endTrace();
    }

    //@Scheduled(cron="0 0/5 * * * ?")
    @Async
    @Scheduled(cron="0 20 0 ? * SUN")
    public void weekLineScheduled() {
        LogTrace.beginTrace("周一0点20执行周k定时任务weekLineScheduled");
        LocalDateTime dateDate = LocalDateTime.now();
        while (dateDate.getDayOfWeek() != DayOfWeek.SUNDAY){
            dateDate = dateDate.minusDays(1);
        }
        LogTrace.info(LogTraceKeysUtils.KLINE_WEEK_SCHEDULED, LogTraceKeysUtils.DATA,"周一0点20执行周k定时任务weekLineScheduled");
        kLineService.weekLineScheduled(KLineTypeEnum.WEEK , dateDate);
        LogTrace.info(LogTraceKeysUtils.KLINE_WEEK_SCHEDULED, LogTraceKeysUtils.DATA, "周一0点20执行周k定时任务weekLineScheduled定时执行完成");
        LogTrace.endTrace();
    }*/
}
